package com.hm.PushCaseService.service.user;

import com.hm.PushCaseService.mapper.user.UserMapper;
import com.hm.PushCaseService.pojo.response.UserBean;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;


@Service
public class UserService implements IUserService {

    //默认有效期是10分钟
    private static final long SURVIVAL_TIME = 60 * 10;

    @Qualifier("MainSqlSessionFactory")
    private final SqlSessionFactory sqlSessionFactory;

    @Qualifier("jedisPool")
    private final JedisPool jedisPool;

    public UserService(JedisPool jedisPool, SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        this.jedisPool = jedisPool;
    }

    @Override
    public UserBean getUserForName(String userName) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("userName", userName);
            return mapper.getUserForName(params);
        }
    }

    @Override
    public List<UserBean> getAllUser() {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            return mapper.getAllUser();
        }
    }

    @Override
    public void deleteForUserId(Long userId) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            HashMap<String, Long> params = new HashMap<>();
            params.put("userId", userId);
            mapper.deleteForUserId(params);
        }
    }

    @Override
    public void changePasswordForUserId(Long userId, String pwd) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            HashMap<String, String> params = new HashMap<>();
            params.put("userId", userId.toString());
            params.put("userPwd", pwd);
            mapper.changePasswordForUserId(params);
        }
    }

    @Override
    public UserBean getUserForId(Long userId) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            HashMap<String, Long> params = new HashMap<>();
            params.put("userId", userId);
            return mapper.getUserForId(params);
        }
    }

    @Override
    public void addUser(String userName, String userPwd) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            UserMapper mapper = session.getMapper(UserMapper.class);
            UserBean params = new UserBean();
            params.userId = generateUserID();
            params.userName = userName;
            params.userPwd = userPwd;
            params.userPermission = 3;//权限最低
            mapper.addUser(params);
        }
    }

    @Override
    public void recordsUser(Long userId) {
        try (Jedis jedis = jedisPool.getResource()) {
            String key = "create_user_" + userId;
            jedis.setex(key, SURVIVAL_TIME, "1");
        }
    }

    @Override
    public boolean queryRecord(Long userId) {
        try (Jedis jedis = jedisPool.getResource()) {
            String key = "create_user_" + userId;
            boolean exists = jedis.exists(key);
            if (!exists) {
                //不存在，说明过了限制时间
                return true;
            }
            String value = jedis.get(key);
            if (value == null || value.isEmpty()) {
                //说明过了限制时间
                return true;
            }
        }
        return false;
    }

    private Long generateUserID() {
        long time = new Date().getTime();
        int i = new Random().nextInt(100000);
        return time * i;
    }

}
